﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using Utils;
using System.IO;

public partial class Admin_Pages_OrderBillDetail : System.Web.UI.Page
{
    protected object statusorder
    {
        get
        {
            return ViewState["statusvs"];
        }
        set
        {
            ViewState["statusvs"] = value;
        }
    }
    static Label lblTotal;

    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (!IsPostBack)
        {
            if (Session["login"] != null)
            {
                int oid = Convert.ToInt32(Request.Params["oid"]);
                OrderBillUI orderUI = new OrderBillUI();
                OrderDetailUI orderdetailUI = new OrderDetailUI();
                DataSet dsOrder = orderUI.Get(oid);
                DataSet dsOrderDetail = orderdetailUI.Get(oid);

                lblOrderID.Text = oid.ToString();
                lblDate.Text = Format.FormatVietNameDateSQLSyntax(Convert.ToDateTime(dsOrder.Tables[0].Rows[0]["O_Time"].ToString())).ToString();
                lblName.Text = dsOrder.Tables[0].Rows[0]["O_NameCust"].ToString();
                lblPhone.Text = dsOrder.Tables[0].Rows[0]["O_Phone"].ToString();
                lblAddress.Text = dsOrder.Tables[0].Rows[0]["O_Address"].ToString();
                lblPaymentMethod.Text = dsOrder.Tables[0].Rows[0]["PM_Name"].ToString();
                byte status = Convert.ToByte(dsOrder.Tables[0].Rows[0]["Status"].ToString());
                statusorder = status;
                switch (status)
                {
                    case 0:
                        chkPay.Checked = false;
                        chkPay.Enabled = true;
                        chkShipped.Enabled = false;
                        chkShipped.Checked = false;
                        chkReject.Enabled = true;
                        chkReject.Checked = false;
                        Button1.Enabled = true;
                        Button2.Enabled = true;
                        btnRecover.Visible = false;
                        break;
                    case 1:
                        chkPay.Checked = true;
                        chkPay.Enabled = false;
                        chkShipped.Checked = false;
                        chkShipped.Enabled = true;
                        chkReject.Checked = false;
                        chkReject.Enabled = false;
                        Button1.Enabled = true;
                        Button2.Enabled = true;
                        btnRecover.Visible = false;
                        break;
                    case 2:
                        chkPay.Checked = true;
                        chkPay.Enabled = false;
                        chkShipped.Checked = true;
                        chkShipped.Enabled = false;
                        chkReject.Checked = false;
                        chkReject.Enabled = false;
                        Button1.Enabled = false;
                        Button2.Enabled = false;
                        btnRecover.Visible = false;
                        break;
                    case 3:
                        chkPay.Checked = false;
                        chkPay.Enabled = false;
                        chkShipped.Checked = false;
                        chkShipped.Enabled = false;
                        chkReject.Checked = true;
                        chkReject.Enabled = false;
                        Button1.Enabled = false;
                        Button2.Enabled = false;
                        btnRecover.Visible = true;
                        break;
                    default:
                        break;
                }

                lblTotal = new Label();
                double total = 0.0d;

                for (int i = 0; i < dsOrderDetail.Tables[0].Rows.Count; i++)
                {
                    int q = Convert.ToInt32(dsOrderDetail.Tables[0].Rows[i]["Quantity"].ToString());
                    double p = Convert.ToDouble(dsOrderDetail.Tables[0].Rows[i]["Price"].ToString());
                    total += p * q;
                }
                lblTotal.Text = total + "";

                gvOrderDetail.DataSource = dsOrderDetail;
                gvOrderDetail.DataBind();
            }
            else
            {
                Response.Redirect("../Login.aspx");
            }
        }
    }


    protected string CalTotal(string price,string quantity)
    {
        double d = 0.0d;
        double p = Convert.ToDouble(price);
        int q = Convert.ToInt32(quantity);
        d = p * q;
        return d+"";
    }

    protected void gvOrderDetail_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {

            GridView gvFooter = (GridView)sender;
            GridViewRow gvRow = new GridViewRow(gvOrderDetail.Rows.Count, 0, DataControlRowType.Footer, DataControlRowState.Insert);
            TableCell tableCell = new TableCell();
            Label lblTitle = new Label();
            lblTitle.Text = "Total All:";
            Label lblEmpty = new Label();
            lblEmpty.Text = "";
            MergeColumn(gvRow, tableCell, 5, lblTitle, HorizontalAlign.Right);
            MergeColumn(gvRow, tableCell, 1, lblTotal, HorizontalAlign.Left);
            
            gvFooter.Controls[0].Controls.AddAt(gvOrderDetail.Rows.Count + 1, gvRow);
        }
    }

    protected static void MergeColumn(GridViewRow gvRow, TableCell tbCell, int colSpan, Label txt, HorizontalAlign ha)
    {
        tbCell = new TableCell();
        tbCell.Text = txt.Text;
        tbCell.ColumnSpan = colSpan;
        tbCell.HorizontalAlign = ha;
        gvRow.Cells.Add(tbCell);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int oid = Convert.ToInt32(Request.Params["oid"]);
        OrderBillUI orderUI = new OrderBillUI();
        DataSet dsOrder = orderUI.Get(oid);
        byte status = Convert.ToByte(dsOrder.Tables[0].Rows[0]["Status"].ToString());
        byte oldStatus = (byte)statusorder;
        

        if (status != oldStatus)
        {
            Response.Write("<script>alert('data has changed'); window.location='../Pages/OrderBillDetail.aspx?oid="+oid.ToString()+"'</script>");
        }
        else
        {
            if (chkPay.Checked && !chkShipped.Checked && !chkReject.Checked)
            {
                int result= orderUI.ChangeStatus(oid, 1);
                if (result > 0)
                {
                    Response.Write("<script>alert('change status success'); window.location='../Pages/OrderBillList.aspx'</script>");
                }
                else
                {
                    Response.Write("<script>alert('change status fail'); window.location='../Pages/OrderBillList.aspx'</script>");

                }
            }
            else if (chkPay.Checked && chkShipped.Checked && !chkReject.Checked)
            {
                int result = orderUI.ChangeStatus(oid, 2);
                if (result > 0)
                {
                    string folder = dsOrder.Tables[0].Rows[0]["FolderName"].ToString();
                    string path = Server.MapPath("~/Upload/orderbill/" + folder);
                    if (Directory.Exists(path))
                    {
                        foreach (string item in Directory.GetFiles(path))
                        {
                            File.Delete(item);
                        }
                        Directory.Delete(path);
                    }
                    Response.Write("<script>alert('change status success'); window.location='../Pages/OrderBillList.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('change status fail'); window.location='../Pages/OrderBillList.aspx';</script>");
                }
            }
            else if (!chkPay.Checked && !chkShipped.Checked && chkReject.Checked)
            {
                int result = orderUI.ChangeStatus(oid, 3);
                if (result > 0)
                {
                    Response.Write("<script>alert('change status success'); window.location='../Pages/OrderBillList.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('change status fail'); window.location='../Pages/OrderBillList.aspx';</script>");

                }
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int oid = Convert.ToInt32(Request.Params["oid"]);
        OrderBillUI orderUI = new OrderBillUI();
        DataSet dsOrder = orderUI.Get(oid);
        string folder = dsOrder.Tables[0].Rows[0]["FolderName"].ToString();
        Response.Redirect("../Pages/OrderBillOpenFolder.aspx?oid="+oid+"&folder="+folder);
    }
    protected void chkPay_CheckedChanged(object sender, EventArgs e)
    {
        if (chkPay.Checked)
        {
            chkReject.Checked = false;
        }
        else if (chkReject.Checked)
        {
            chkPay.Checked = false;
        }
    }
    protected void chkReject_CheckedChanged(object sender, EventArgs e)
    {
        if (chkReject.Checked)
        {
            chkPay.Checked = false;
        }
        else
            if (chkPay.Checked)
            {
                chkReject.Checked = false;
            }
       
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Pages/OrderBillList.aspx");
    }
    protected void btnRecover_Click(object sender, EventArgs e)
    {
        int oid = Convert.ToInt32(Request.Params["oid"]);
        OrderBillUI orderUI = new OrderBillUI();
        DataSet dsOrder = orderUI.Get(oid);
        byte status = Convert.ToByte(dsOrder.Tables[0].Rows[0]["Status"].ToString());
        byte oldStatus = (byte)statusorder;
        if (status != oldStatus)
        {
            Response.Write("<script>alert('data has changed'); window.location='../Pages/OrderBillDetail.aspx?oid=" + oid.ToString() + "'</script>");
        }
        else
        {
            int result = orderUI.ChangeStatus(oid,0);
            if (result > 0)
            {
                Response.Write("<script>alert('change status success'); window.location='../Pages/OrderBillList.aspx';</script>");
            }
            else
            {
                Response.Write("<script>alert('change status fail'); window.location='../Pages/OrderBillList.aspx';</script>");

            }
        }
    }
}
